package javacommon.util;

import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
@SuppressWarnings("unchecked")
public class TextTool {
	private TextTool(){};
	
	public static String dateToStr(Date date,String pattern) {
		String ptn = isNull(pattern) ? "yyyy-MM-dd" : pattern;
		SimpleDateFormat sdf = new SimpleDateFormat(ptn);
		return sdf.format(date);
	}
	
	/**
	 * 对象是空吗？
	 * @param obj
	 * @return
	 */
	public static boolean isNull(Object obj) {
		boolean flag = false;
		if(obj == null) return true;
		if(obj instanceof String) {
			String temp = (String)obj;
			flag = ("".equals(temp.trim()) ? true :false);
		} else if(obj instanceof Collection) {
			Collection temp = (Collection) obj;
			flag = temp.isEmpty();
		}
		return flag;
	}
	
	/**
	 * 对象不是空
	 * @param obj
	 * @return
	 */
	public static boolean isNotNull(Object obj) {
		boolean flag = false;
		if(obj == null) return false;
		if(obj instanceof String) {
			String temp = (String)obj;
			flag = ("".equals(temp.trim()) ? false :true);
		} else if(obj instanceof Collection) {
			Collection temp = (Collection) obj;
			flag = !temp.isEmpty();
		}
		return flag;
	}
	
	public static void main(String[] args) {
		System.out.println(TextTool.dateToStr(new Date(System.currentTimeMillis()), "yyyy-MM-dd hh:mm:ss"));
	}
}
